"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Binary
Case Name   : binary类型不支持创建MOT表
Create At   : 2022/03/21
Owner       : @li-xin12345
Description :
    1. 创建兼容B库
    2. 检查dolphin插件
    3. 设置enable_incremental_checkpoint=off
    4. 创建mot表含binary类型
    5. 恢复环境
Expect      :
    1. 成功
    2. 成功
    3. 设置成功
    4. 创建失败，合理报错
    5. 恢复成功
History     :
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common


class MotSupportBinary(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.commonsh = CommonSH('PrimaryDbUser')
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        self.db_name = 'db_binary_024'
        self.tb_name = 't_binary_024'
        self.error_mes = 'Column type pg_catalog.binary is not supported'
        self.default_vales = self.common.show_param(
            'enable_incremental_checkpoint')

    def test_binary_mot(self):
        text = '-----step1:创建兼容b库;expect:创建成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.commonsh.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      "执行失败" + text)

        text = '-----step2:b库下查询dolphin插件;expect:自动加载,无需手动创建-----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.commonsh.execut_db_sql(sql_cmd,
                                              dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('dolphin', sql_res, '查询失败')
        
        text = '-----step3:设置enable_incremental_checkpoint=off;' \
               'expect:成功-----'
        self.log.info(text)
        if 'off' not in self.default_vales:
            set_off = self.commonsh.execute_gsguc(
                "set", self.constant.GSGUC_SUCCESS_MSG,
                f'enable_incremental_checkpoint=off')
            self.log.info(set_off)
            status = self.commonsh.restart_db_cluster()
            self.log.info(status)
            res = self.common.show_param('enable_incremental_checkpoint')
            self.log.info(res)
            self.assertIn("off", res, "执行失败：" + text)

        text = '-----step4:创建mot表含binary类型;expect:合理报错-----'
        self.log.info(text)
        res = self.commonsh.execut_db_sql(f'''
        drop foreign table if exists {self.tb_name};
        create foreign table {self.tb_name}(v1 binary(10))
        ''', dbname=self.db_name)
        self.log.info(res)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, res,
                      "执行失败：" + text)
        self.assertIn(self.error_mes, res, "执行失败：" + text)

    def tearDown(self):
        text = '-----step5:恢复环境;expect:成功-----'
        self.log.info(text)
        sql_res = self.commonsh.execut_db_sql(
            f"drop database if exists {self.db_name};", dbname='postgres')
        self.log.info(sql_res)
        restores_default_value = self.commonsh.execute_gsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            f"enable_incremental_checkpoint={self.default_vales}")
        self.log.info(restores_default_value)
        restart_res = self.commonsh.restart_db_cluster()
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      "执行失败：" + text)
        self.assertTrue(restores_default_value, "执行失败:" + text)
        self.assertTrue(restart_res, '执行失败:' + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")